<%@page import="java.sql.Statement"%>
<%@page import="java.sql.SQLException"%>
<%@page import="java.sql.ResultSet"%>
<%
      String data = "";
       String sql = "";
       String json = "";
       int errorcode = 0;
       ResultSet res = null;
       model.ConnectionDB connection = new model.ConnectionDB();

        if(request.getParameter("Action").equals("Drive1")){
            sql = "SELECT * FROM seekdriversprin();";
            //out.print(sql);
            try {
                connection.connect();
                res = connection.consult(sql);
                connection.getCon().close();

                while (res.next()) {
            //if(res.next()){
            //HttpSession sessionUser=request.getSession();//Create new Session
            //sessionUser.setAttribute("result",res);
                    if (!data.equals("")) {
                        data += ",";
                    }
                    data += '{';
                    data += "'i' :" + res.getInt(1);
                    //data += "'state' :'" + res.getString("city_state") + "', ";
                    //data += "'idCity': " + res.getInt("city_id") + ", ";
                    data += '}';
                }
            }catch(Exception e) {
                out.print(e.getMessage());
                out.print("{'errorcode' : -1}"); //
                connection.getCon().close();
            }
        } //Incorrect Access
       else  if(request.getParameter("Action").equals("Drivee")){
            sql = "SELECT * FROM seekdriversre();";
            //out.print(sql);
            try {
                connection.connect();
                res = connection.consult(sql);
                connection.getCon().close();

                while (res.next()) {
            //if(res.next()){
            //HttpSession sessionUser=request.getSession();//Create new Session
            //sessionUser.setAttribute("result",res);
                    if (!data.equals("")) {
                        data += ",";
                    }
                    data += '{';
                    data += "'d' :" + res.getInt(1);
                    //data += "'state' :'" + res.getString("city_state") + "', ";
                    //data += "'idCity': " + res.getInt("city_id") + ", ";
                    data += '}';
                }
            }catch(Exception e) {
                out.print(e.getMessage());
                out.print("{'errorcode' : -1}"); //
                connection.getCon().close();
            }
        } //Incorrect Access
       else if(request.getParameter("Action").equals("Add")){
            sql = "SELECT addvehicles(" + request.getParameter("MODEL") +  ",'" + request.getParameter("PLATE") + "'," + request.getParameter("CAPACITY") + "," + request.getParameter("NAMEPRIN") +"," + request.getParameter("NAMERE")+",'" + request.getParameter("TYPE")+"')";
                try {
                            connection.connect();
                            res=connection.consult(sql);
                            
                            connection.getCon().close();
                            
                            if(res.next())
                              //  out.print(sql);
                              data="{'ans' : " + res.getInt(1) + "}";//Add  "'ori' :'" + res.getString(2) + "', ";
                    } catch (Exception e) {
                           errorcode =-5;
                    } 
            }
       else if(request.getParameter("Action").equals("Update")){
            sql = "SELECT updatevehicles(" + request.getParameter("MODEL") +  ",'" + request.getParameter("PLATE") + "'," + request.getParameter("CAPACITY") + "," + request.getParameter("NAMEPRIN") +"," + request.getParameter("NAMERE")+",'" + request.getParameter("TYPE")+"')";
                try {
                            connection.connect();
                            res=connection.consult(sql);

                            connection.getCon().close();

                            if(res.next())
                              //  out.print(sql);
                              data="{'ans' : " + res.getInt(1) + "}";//Add  "'ori' :'" + res.getString(2) + "', ";
                    } catch (Exception e) {
                           errorcode =-5;
                    }
            }
       else if(request.getParameter("Action").equals("Delete")){
            sql = "SELECT deletevehicles('" + request.getParameter("PLATE")+ "')";
            try {
                            connection.connect();
                            res=connection.consult(sql);

                            connection.getCon().close();
                            //out.print(sql);

                            if(res.next())
                               // out.print(sql);
                             data="{'ans' : " + res.getInt(1) + "}";//Add  "'ori' :'" + res.getString(2) + "', ";
                    } catch (Exception e) {
                           errorcode =-5;
                    }
            }

       json = "{'errorcode' : " + errorcode + ", 'data' : [" + data + "]}";
       out.print(json);
%>